<?php
class SettingsController extends AppController {
	public $name = 'Settings';

    public function admin_index () {
        $params = array(
            //'conditions' => 'Setting.short_name != "theme"',
            //'order'      => 'Setting.category ASC, Setting.position ASC'
            'order'      => 'Setting.id ASC'
        );

        $data = $this->Setting->find('all', $params);
        $list = '';

        foreach ($data as $i=>$a) {
            if (substr($a['Setting']['params'], 0, 8) == 'fields=>') {
                $fields = r('fields=>', '', $a['Setting']['params']);
                $pos    = strpos($fields, '.');
                $model  = substr($fields, 0, $pos);

                App::import('Model', $model);
                $obj  = new $model();
                $list = $obj->find('list', array('fields'=>$fields, 'order'=>'id ASC'));

                $data[$i]['Setting']['params'] = $list;
            } else if ($a['Setting']['params']) {
                $fields = explode("\r\n", $a['Setting']['params']);
                $list   = array();

                foreach ($fields as $field) {
                    list($key, $val) = split('=', $field);
                    $list[$key] = __($val, true);
                }

                $data[$i]['Setting']['params'] = $list;
            } else {
                $list = '';
            }

            $attributes = array('label' => false);
            if ($a['Setting']['attributes']) {
                $fields = explode("\r\n", $a['Setting']['attributes']);

                foreach ($fields as $field) {
                    list($key, $val)  = split('=', $field);
                    $attributes[$key] = __($val, true);
                }
            }

            App::import('Helper', 'Form');
            $form = new FormHelper();

            if ($a['Setting']['field_type'] == 'radio') {
                $attributes['legend'] = false;
                $attributes['value']  = $a['Setting']['value'];
                $data[$i]['Setting']['attributes'] = $attributes;
                $data[$i]['Setting']['options']    = $list;
            } else {
                if ($list) {
                    $attributes['type']    = 'select';
                    $attributes['options'] = $list;
                    $attributes['default'] = $a['Setting']['value'];
                } else {
                    $attributes['type']  = 'text';
                    $attributes['value'] = $a['Setting']['value'];
                }
                $data[$i]['Setting']['options'] = $attributes;
            }
        }

        $this->set('data', $data);
    } // admin_index

    function admin_save () {
        if ($this->data) {
            foreach ($this->data['Setting'] as $data) {
                $this->Setting->id = $data['id'];
                $this->Setting->save(array('Setting'=>$data));
            }
            $this->Session->setFlash('<h4>'.__('Settings has been saved',true).'</h4>', 'default', array('class'=>'boxSuccess'));
        }
        $this->redirect('/admin/settings');
    } // admin_save

    function index () {
        $params = array('order' => 'Setting.category ASC, Setting.position ASC');
        return $this->Setting->find('all', $params);
    } // index
} // SettingsController
?>